package org.linlinjava.litemall.admin.service;

import com.google.common.collect.Lists;
import org.linlinjava.litemall.admin.beans.vo.LumierePayMemberVo;
import org.linlinjava.litemall.admin.util.PageUtil;
import org.linlinjava.litemall.core.util.ResponseUtil;
import org.linlinjava.litemall.core.validator.Order;
import org.linlinjava.litemall.core.validator.Sort;
import org.linlinjava.litemall.db.domain.LumierePayMemberUserVo;
import org.linlinjava.litemall.db.service.PayMemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 管理成员用户服务
 *
 * @author yliyun
 * @date 2022/09/14
 */
@Service
public class AdminPayMemberUserService {

    /**
     * 付费会员服务
     */
    @Autowired
    private PayMemberService payMemberService;

    /**
     * 查询支付会员用户
     *
     * @param userName   用户名
     * @param memberName 成员名
     * @param status     状态
     * @param page       页面
     * @param limit      限制
     * @param sort       排序
     * @param order      订单
     * @return {@link Object}
     */
    public Object queryPayMemberUser(String userName, String memberName, Byte status,
                                     Integer page, Integer limit, String sort, String order) {
        List<LumierePayMemberUserVo> vos = payMemberService.queryPayMemberUsers(userName, memberName, page, limit, sort, order);


        vos.stream().forEach(vo -> {
            LocalDateTime endTime = vo.getEndTime();
            if (LocalDate.now().isBefore(endTime.toLocalDate())) {
                vo.setStatus((byte) 1);
            } else {
                vo.setStatus((byte) 2);
            }
        });

        if (status != null) {
            vos = vos.stream().filter(vo -> {
                if (vo.getStatus().equals(status)) {
                    return true;
                } else {
                    return false;
                }
            }).collect(Collectors.toList());
        }


        Pageable pageable = new PageRequest(page - 1,limit);
        PageImpl<LumierePayMemberUserVo> list = PageUtil.getPageLumierePayMemberUserLogVo(pageable,vos);
        return ResponseUtil.ok(list);
    }


}
